<?php

abstract class Mac_Html_Tag_Abstract
{
    const OPT_ID = 'id';
    const OPT_CLASS = 'class';
    
    protected $hasCloseTag = true;
    protected $content = null;
    protected $options = array();
    protected $tag = null;
    
    public function __construct($options = array(), $content = null)
    {
        foreach ($options as $opt_name => $opt_value) {
            $this->addOption($opt_name, $opt_value);
        }
        $this->content = $content;
    }
    
    protected function hasOption ($option)
    {
        $refl = new ReflectionClass(get_called_class());
        return (in_array($option, $refl->getConstants()));
    }
    
    protected function addOption ($name, $value)
    {
        if ($this->hasOption($name)) {
            $this->options[$name] = $value;
        }
    }
    
    public function setContent ($content)
    {
        $this->content = $content;
    }
    
    public function __toString()
    {
        $str = '<'.$this->tag;
        foreach ($this->options as $opt_name => $opt_value) {
            $str .= ' '.$opt_name.'="'.$opt_value.'"';
        }
        if ($this->hasCloseTag === true) {
            $str .= '>';
            $str .= $this->content;
            $str .= '</'.$this->tag.'>';
        } else {
            $str .= ' />';
        }
        return $str;
    }
}